package flow.controller;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class CodeTest {

    @Test
    public void createEngine() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration
                .createStandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://localhost:3306/demo_flowable?useUnicode=true&characterEncoding=utf8")
                .setJdbcDriver("com.mysql.jdbc.Driver")
                .setJdbcUsername("root")
                .setJdbcPassword("123456")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        //DB_SCHEMA_UPDATE_FALSE //不会自动创建表，没有表，则抛异常
        //DB_SCHEMA_UPDATE_CREATE_DROP //先删除，再创建表
        //DB_SCHEMA_UPDATE_TRUE //假如没有表，则自动创建
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("流程引擎创建成功!");
        InputStream inputStreamBpmn = this.getClass().getResourceAsStream("/processes/countersign.bpmn20.xml");
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .addInputStream("countersign.bpmn20.xml", inputStreamBpmn)//
                .name("会签")
                .deploy();
        System.out.println("部署ID：" + deployment.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("user1", "user1");
        map.put("user2", "user2");
        map.put("user3", "user3");
        ProcessInstance pi = processEngine.getRuntimeService()//
                .startProcessInstanceByKey("countersign", map);//使用流程定义的key的最新版本启动流程
        System.out.println("流程实例ID：" + pi.getId());
        System.out.println("流程定义的ID：" + pi.getProcessDefinitionId());
    }

}
